package com.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExeCopyIOSPngForAndroid {
	static String hdpiPath,xhdpiPath,xxhdpiPath;
	
	

	public static void main(String[] args) {
		String str = "C:\\Users\\THINK\\Desktop\\帐单和积分里图标";
		String location = str.replaceAll("\\\\", "/");
		System.out.println(location);
		File hdpi,xhdpi,xxhdpi,srcFile;
		hdpi = new File(location, "drawable-hdpi-v4");
		if (!hdpi.exists()) {
			hdpi.mkdirs();
		}
		hdpiPath=hdpi.getAbsolutePath();
		
		xhdpi = new File(location, "drawable-xhdpi-v4");
		if (!xhdpi.exists()) {
			xhdpi.mkdirs();
		}
		
		xhdpiPath=xhdpi.getAbsolutePath();
		xxhdpi = new File(location, "drawable-xxhdpi-v4");
		if (!xxhdpi.exists()) {
			xxhdpi.mkdirs();
		}
		xxhdpiPath=xxhdpi.getAbsolutePath();
		srcFile = new File(location);
		getAllFiles(srcFile,0);
	}

	// 获取层级的方法
	public static String getLevel(int level) {
		// A mutable sequence of characters.
		StringBuilder sb = new StringBuilder();
		for (int l = 0; l < level; l++) {
			sb.append("|--");
		}
		return sb.toString();
	}

	public static void getAllFiles(File dir, int level) {
		System.out.println(getLevel(level) + dir.getName());
		level++;
		System.out.println(dir.exists());
		if (dir.isDirectory()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				File file=files[i];
				if (file.isDirectory()) {
					if(file.getAbsolutePath().contains("hdpi")){
						continue;
					}
					// 这里面用了递归的算法
					getAllFiles(files[i], level);
				} else {
					System.out.println(getLevel(level) + file);
					String fileName = file.getName();
					if(fileName.contains("@1.5x")){
						fileName="ic_"+fileName.replaceAll("@1.5x", "");
						copyFile(file,new File(hdpiPath,fileName));
					}else if(fileName.contains("@2x")){
						fileName="ic_"+fileName.replaceAll("@2x", "");
						copyFile(file,new File(xhdpiPath,fileName));
					}else if(fileName.contains("@3x")){
						fileName="ic_"+fileName.replaceAll("@3x", "");
						copyFile(file,new File(xxhdpiPath,fileName));
					}else{
						fileName="ic_"+fileName;
						copyFile(file,new File(hdpiPath,fileName));
					}
				}
			}
		}
	}

	
	public static void getDir(String strPath) throws Exception {
		// java文件搜索
		try {
			File f = new File(strPath);
			if (f.isDirectory()) {
				File[] fList = f.listFiles();
				for (int j = 0; j < fList.length; j++) {
					if (fList[j].isDirectory()) {
						System.out.println(fList[j].getPath());
						getDir(fList[j].getPath()); // 在getDir函数里面又调用了getDir函数本身
					}
				}
				for (int j = 0; j < fList.length; j++) {
					if (fList[j].isFile()) {
						System.out.println(fList[j].getPath());// 把这话换成你要处理的语句.比如
																// fList[j].substring(fList[j].length-3,3)=="txt"
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static boolean copyFile(File fileOld,File fileNew) {
		// java 文件与文件夹拷贝
//		File fileOld = new File(srcPath);
//		File fileNew = new File(targetPath);
		if (fileOld.exists()) {
			try {
				FileInputStream fis = new FileInputStream(fileOld);
				FileOutputStream fos = new FileOutputStream(fileNew);
				int read = 0;
				while ((read = fis.read()) != -1) {
					fos.write(read);
					fos.flush();
				}
				fos.close();
				fis.close();
				return true;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	public static boolean renameFile(String srcFile,String targetFile) {
		// java文件重命名
		File file = new File(srcFile);
		if (file.exists()) {
			return file.renameTo(new File(targetFile));
		}
		return false;
	}

}
